from flask import Blueprint, request

from model.Blog import Blog
from service.blog_service import BlogService
from utils.ResultUtil import Response
from model.User import User
blog = Blueprint("blog", __name__, url_prefix="/blog")


@blog.route('/list', methods=['GET'])
def blog_list():
    page = int(request.args.get("page"))
    print(page)
    data = BlogService().list(page)
    return Response().success(data)

@blog.route('/count', methods=['GET'])
def count():
    return Response().success(BlogService().getCountBlog())

@blog.route('/getById', methods=['GET'])
def byId():
    id = request.args.get("id")
    return Response().success(BlogService().getById(id))

@blog.route('/comments',methods=['GET'])
def getComments():
    return Response().success(BlogService().getIndexComments())

@blog.route('/getTheme',methods=['GET'])
def getTheme():
    return Response().success(BlogService().getTheme())

@blog.route('/putImg',methods=['POST'])
def putImg():
    data = request.get_json()

    return Response().success(BlogService().putImg(data["img"]))

@blog.route('/addBlog',methods=['POST'])
def addBlog():
    data = request.get_json()
    return Response().success(BlogService().addBlog(Blog.decode(data)))

@blog.route('/deleteById', methods=['GET'])
def delete():
    id = request.args.get("id")
    return Response().success(BlogService().deleteFoodByID(id))

@blog.route('/update', methods=['POST'])
def updateById():
    data = request.get_json()
    id = request.get_json()["id"]
    return Response().success(BlogService().updateByID(Blog.decode(data), id))



